package com.nassir.xml.xmlreader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.nassir.excel.model.File;
import com.nassir.excel.model.Sheet;
import com.nassir.excel.model.WorkBook;

/**
 * xml读取
 * @author zhangqiulong
 *
 */
public class XmlReader {
    private WorkBook workbook = null;

    public XmlReader(java.io.File file) throws Exception {
        try {
            IXmlTransformer xml = new XmlTransformer();
            this.workbook = (WorkBook) xml.xmlToModel(new FileInputStream(file.getAbsolutePath()), WorkBook.class);
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("xml文件不存在或路径错误");
        }
    }

    /**
     * 获取�?��的Sheets�?
     * @return
     * @throws Exception
     */
    public Map<String, Sheet> getSheets() throws Exception {
        Map<String, Sheet> sheetmap = new HashMap<String, Sheet>();
        List<Sheet> sheets = workbook.getSheets();
        if (null != sheets && !sheets.isEmpty()) {
            for (Sheet sheet : sheets) {

                sheetmap.put(sheet.getName(), sheet);
            }
        }

        return sheetmap;
    }

    /**
     * 获取�?��的Sheets�?
     * @return
     * @throws Exception
     */
    public Sheet getSheets(String name) throws Exception {
        Sheet retsheet = null;

        List<Sheet> sheets = workbook.getSheets();

        if (null != sheets && !sheets.isEmpty()) {
            for (Sheet sheet : sheets) {
                if (name.equals(sheet.getName())) {
                    retsheet = sheet;
                    break;
                }
            }
        }

        return retsheet;
    }

    /**
     * 获取�?��的file(未转换过�?
     * @return
     * @throws Exception
     */
    private Map<String, File> getFiles() throws Exception {
        Map<String, File> sheetmap = new HashMap<String, File>();
        List<File> files = workbook.getFiles();
        if (null != files && !files.isEmpty()) {
            for (File file : files) {
                sheetmap.put(file.getName(), file);
            }
        }

        return sheetmap;
    }

    /**
     * 获取指定的file(转换过的)
     * @return
     * @throws Exception
     */
    public File getFile(String name) throws Exception {
        File newfile = new File();
        //获取�?��的file
        Map<String, File> files = this.getFiles();
        //获取�?��的sheet�?
        Map<String, Sheet> allsheet = this.getSheets();
        //获取当前file
        File file = files.get(name);

        if (null == file) {
            throw new IllegalArgumentException("file:" + name + "未找到");
        }
        //获取当前file的所有sheet�?
        List<Sheet> sheets = file.getSheets();

        List<Sheet> newsheets = new ArrayList<Sheet>();
        //ref-name 名称到对象的转换
        if (null != sheets && !sheets.isEmpty()) {
            for (Sheet sheet : sheets) {
                Sheet refsheet = allsheet.get(sheet.getRefName());
                if (null == refsheet) {
                    throw new IllegalArgumentException("sheet页配置有(ref-name:" + sheet.getRefName()
                            + ")找不到对应的sheet页！");
                }
                newfile.setName(refsheet.getName());
                newsheets.add(refsheet);
            }
        }
        newfile.setName(file.getName());
        newfile.setSheets(newsheets);
        return newfile;
    }

    /**
     * 获取�?��的file(转换过的)
     * @return
     * @throws Exception
     */
    public List<File> getAllFiles() throws Exception {
        List<File> newfiles = new ArrayList<File>();

        List<File> files = workbook.getFiles();
        if (null != files && !files.isEmpty()) {
            for (File f : files) {
                newfiles.add(this.getFile(f.getName()));
            }
        }
        return newfiles;
    }

    public static void main(String[] args) throws FileNotFoundException, Exception {
        java.io.File file = new java.io.File("src/workbookconfig1.xml");
        XmlReader reader = new XmlReader(file);
        File f = reader.getFile("报价");
        System.out.println(f.getName());
        System.out.println(f.getSheets().size());

        for (Sheet s : f.getSheets()) {
            System.out.println(s.getName());
        }
    }

    public WorkBook getWorkbook() {
        return workbook;
    }

    public void setWorkbook(WorkBook workbook) {
        this.workbook = workbook;
    }

}
